<?php
include ("global.php");

//board close check
if($configarray[40]){
	$links=template('linkbarseparator').'Post';
	$pagetitle=template('pagetitleseparator').'Post';

	include("header.php");


	echo template('noticeboardclosed',array('notice'=>$configarray[40]));
}else{

	if(isset($topic)){
		$topicnum=$topic;
		$topictime=topic_numtotime($forum,$topicnum);
	}

	$forumconfigarray=getdata("$configarray[2]/$forum.php");
	$topicconfigarray=getdata("$configarray[2]/$forum/$topictime.php");
	$postarray=listfiles("$configarray[2]/$forum/$topictime");
	$firstpostarray=getdata("$configarray[2]/$forum/$topictime/0.php");


	if(!isset($forum)){
		$pagetitle=template('pagetitleseparator').'Post';
		$links=template('linkbarseparator').'Post';
	}

	if(isset($forum)&&!isset($topic)){
		$pagetitle=template('pagetitleseparator').$forumconfigarray[3].template('pagetitleseparator').'New topic';
		$links=template('linkbarseparator')."<a href=\"index.php?forum=$forum\">$forumconfigarray[3]</a>".template('linkbarseparator').'New topic';
	}

	$firstpostarray[2]=htmlentities($firstpostarray[2]);

	if(isset($forum)&&isset($topictime)){
		if($mode=="edit"){
			$pagetitle=template('pagetitleseparator').$forumconfigarray[3].template('pagetitleseparator').$firstpostarray[2].template('pagetitleseparator').'Editing';
			$links=template('linkbarseparator')."<a href=\"index.php?forum=$forum\">$forumconfigarray[3]</a>".template('linkbarseparator')."<a href=\"index.php?forum=$forum&topic=$topic\">$firstpostarray[2]</a>".template('linkbarseparator').'Editing';
		}else{
			$pagetitle=template('pagetitleseparator').$forumconfigarray[3].template('pagetitleseparator').$firstpostarray[2].template('pagetitleseparator')."New reply";
			$links=template('linkbarseparator')."<a href=\"index.php?forum=$forum\">$forumconfigarray[3]</a>".template('linkbarseparator')."<a href=\"index.php?forum=$forum&topic=$topic\">$firstpostarray[2]</a>".template('linkbarseparator')."New reply";
		}
	}

	include ("header.php");

	if(isset($forum)&&isset($topictime)&&!isset($post)&&!$mode){$mode="reply";}
	if(isset($forum)&&!isset($topictime)&&!isset($post)&&!$mode){$mode="topic";}

	$emailarray=explode("\t",$userloggedinarray[2]);
	if(checkban($userkeyarray[$navboardlogin],$REMOTE_ADDR,$emailarray[0],$userloggedinarray[15])){//check ban
	echo template('noticebanned');
	}else{//check ban

	//post access
	if(($mode=="topic"&&!getforumaccess("topic")) || (($mode=="reply"||$mode=="quote")&&!getforumaccess("reply")) || (isset($forum)&&isset($topictime)&&($topicconfigarray[0]=="closed"&&$userloggedinarray[15]!=="administrator"))){

		echo template('noticepostnoaccess');

	}elseif(count($forumconfigarray)<=0||count($postarray)<=0){

		echo template('noticeforumortopicinvalid');

	}else{

		if($step==""){//step1
		$moderatorsarray=explode(",",$forumconfigarray[1]);
		$post--;
		$indpostarray=getdata("$configarray[2]/$forum/$topictime/$post.php");

		if($mode=="edit"&&!geteditaccess()){//edit access
		echo template('noticeeditnoaccess');
		}else{//edit access

		//tableheader1();

		$file_uploads=ini_get("file_uploads");

		$tplcontent['formheader'].="<form action=\"post.php\" ";

		//if file_uploads in phpini
		if($file_uploads){
			$tplcontent['formheader'].="enctype=\"multipart/form-data\" ";
		}

		$tplcontent['formheader'].="method=post name=\"post\">";

		$tplcontent['formheader'].="<input type=hidden name=\"step\" value=\"2\">";
		if(isset($forum)) {$tplcontent['formheader'].="<input type=hidden name=\"forum\" value=\"$forum\">";}
		if(isset($topic)) {$tplcontent['formheader'].="<input type=hidden name=\"topic\" value=\"$topicnum\">";}
		if(isset($post)) {$tplcontent['formheader'].="<input type=hidden name=\"post\" value=\"$post\">";}
		if($mode) {$tplcontent['formheader'].="<input type=hidden name=\"mode\" value=\"$mode\">";}
		if(isset($poststart)) {$tplcontent['formheader'].="<input type=hidden name=\"poststart\" value=\"$poststart\">";}

		$tplcontent['formheader'].="<input type=hidden name=\"step\" value=\"2\">";

		unset($value);
		if($mode=="edit"&&isset($post)){
			$value=htmlentities($indpostarray[2]);
		}

		$tplcontent['subject'].="<input type=text name=\"subject\" value=\"$value\" class=\"forminput\" size=80 tabindex=\"1\">";

		$tplcontent['smilies'].=displaysmilies();

		$value="";

		//quote
		if($mode=="quote"&&isset($post)){
			$value=trim(ereg_replace("\[br\]","\n",$indpostarray[3]));
			$poster=$indpostarray[0];
			$userarray=getdata("$configarray[1]/$poster/main.php");
			if(count($userarray)<=0||$poster==0){
				$userarray[0]=template('guest');
			}
			$value="[quote=$userarray[0]]${value}[/quote]";
		}
		//edit
		if($mode=="edit"&&isset($post)){
			$value=ereg_replace("\[br\]","\n",$indpostarray[3]);
			$value=htmlentities($value);
		}

		$tplcontent['body'].="<textarea class=\"forminput\" rows=20 cols=100 name=\"body\" class=\"forminput\" tabindex=\"2\">$value</textarea><br>";

		$bbcodearray=getdata("$configarray[5]/bbcode.php");

		//display bbcode
		if(count($bbcodearray)>0){

			for($n=0;$n<count($bbcodearray);$n++){
				$linearray=explode("\t",$bbcodearray[$n]);

				$tplcontent['bbcode'].="<a href=\"javascript:codeinsert('$linearray[0]')\">$linearray[0]</a><br>";
			}

		}else{
			$tplcontent['bbcode'].="No bbcode available";
		}

		//not edit or new topic
		if($mode!=="edit"&&$mode!=="topic"){
			//rate option

			unset($tplcontent2);
			$tplcontent2['rating'].="<select size=1 name=\"rating\" class=\"forminput\" tabindex=\"4\"><option value=\"\"></option>";

			for($r=10;$r>0;$r--){
				$tplcontent2['rating'].="<option value=\"$r\">$r</option>\n";
			}

			$tplcontent2['rating'].="</select>";

			$tplcontent['rating']=template('posteditrating',$tplcontent2);

		}else{
			$tplcontent['rating'].="";
		}

		if($file_uploads){//file_uploads on in phpini

		$tplcontent2['attachmentvalidext'].=$configarray[22];
		$tplcontent2['attachmentsizelimitkb'].=round(($configarray[23]/1024),2);
		$tplcontent2['attachmentsizelimitb'].=$configarray[23];

		$tplcontent2['attachment'].="<input type=file name=\"attachmentfile\" class=\"forminput\" size=40 tabindex=\"6\">";

		if($mode=="edit"){
			$tplcontent2['attachmentdelete'].="<input type=checkbox name=\"deleteattachment\" class=\"forminput\" size=40 tabindex=\"5\">";
			$tplcontent['attachment'].=template('posteditattachment2',$tplcontent2);
		}else{
			$tplcontent['attachment'].=template('posteditattachment',$tplcontent2);
		}

		}else{

			$tplcontent['attachment'].=template('posteditattachmentdisabled');

		}

		///POLLS (newtopiconly)
		if($mode=="topic"){

			unset($tplcontent2);
			$tplcontent2['checkbox'].="<input type=checkbox name=\"poll\" class=\"forminput\" tabindex=\"7\">";

			for($n=0;$n<$configarray[24];$n++){
				unset($tplcontent3);
				$tplcontent3['optionnum'].=($n+1);
				$tplcontent3['optionbox'].="<input type=text name=\"pollitem$n\" class=\"forminput\" tabindex=\"".(8+$n)."\">";
				$tplcontent2['options'].=template('posteditpolloption',$tplcontent3);
			}

			$tplcontent['poll'].=template('posteditpoll',$tplcontent2);
		}

		///IMPORTANT POST, admin/mod only otherwise keep previous setting
		if(($userloggedinarray[15]=="administrator"||(@in_array($useridarray[$navboardlogin],$moderatorsarray)&&$login==1))&&($post==0||$mode=="topic")){

			unset($tplcontent2);
			if(substr($topictime,0,10)=="important_"){
				$tplcontent2['important'].="<input type=checkbox name=\"importantpost\" checked class=\"forminput\" tabindex=\"".(7+$configarray[24]+1)."\">";
			}else{
				$tplcontent2['important'].="<input type=checkbox name=\"importantpost\" class=\"forminput\" tabindex=\"".(7+$configarray[24]+1)."\">";
			}

			$tplcontent['important'].=template('posteditimportant',$tplcontent2);

		}else{

			if(substr($thread,0,10)=="important_"&&($mode=="reply"||$mode=="edit")){
				$tplcontent['important'].="<input type=hidden name=\"importantpost\" value=\"on\">";
			}

		}

		//disable bbcode
		unset($tplcontent2);
		$tplcontent2['ignorebbcode'].="<input type=checkbox name=\"disablebbcode\" class=\"forminput\" tabindex=\"".(7+$configarray[24]+2)."\" ";
		//if bbcode already disabled keep checked
		if($indpostarray[10]=="on"){
			$tplcontent2['ignorebbcode'].="checked";
		}
		$tplcontent2['ignorebbcode'].=">";

		//add disable bbcode template to main post edit template
		$tplcontent['ignorebbcode'].=template('posteditignorebbcode',$tplcontent2);

		//disable smilies
		unset($tplcontent2);
		$tplcontent2['ignoresmilies'].="<input type=checkbox name=\"disablesmilies\" class=\"forminput\" tabindex=\"".(7+$configarray[24]+3)."\" ";
		//if smilies already disabled keep checked
		if($indpostarray[12]=="on"){
			$tplcontent2['ignoresmilies'].="checked";
		}
		$tplcontent2['ignoresmilies'].=">";

		//add disable smilies template to main post edit template
		$tplcontent['ignoresmilies'].=template('posteditignoresmilies',$tplcontent2);

		$tplcontent['submit'].="<input type=submit name=\"submit\" value=\"Submit\" class=\"formbutton\" tabindex=\"".(7+$configarray[24]+4)."\">";

		$tplcontent['formfooter'].="</form>";

		echo template('postedit',$tplcontent);

		if(isset($topictime)&&isset($forum)){
			$tplcontent['lastposts'].=$configarray[8];

			@rsort($postarray,SORT_NUMERIC);
			for($n=0;$n<count($postarray)&&$n<$configarray[8];$n++)
			{
				$tplcontent['postrows'].=displaypostrow($forum,$topictime,$postarray[$n]);
			}
			@sort($postarray,SORT_NUMERIC);

			echo template("postreview",$tplcontent);
		}//topic review display check

		}//end edit access check bracket
		}//end step1

		#######ADDING THE POST#######
		if($step==2){//step2

		if(isset($topic)){
			$topicnum=$topic;
			$topictime=topic_numtotime($forum,$topicnum);
		}

		$body=ereg_replace("\[br\]","{br}",$body);

		$body=ereg_replace("\n","",$body);
		$body=ereg_replace("\r","[br]",$body);
		$body=stripslashes($body);

		$subject=ereg_replace("\n","",$subject);
		$subject=ereg_replace("\r","",$subject);
		$subject=stripslashes($subject);

		if($login!==1){ //this part finds most recent post time in forum (for guest flood control)
		$newesttopictime=newestthread($forum);
		$userloggedinarray[18]=$newesttopictime;
		}

		//flood control
		if((($userloggedinarray[18]+$configarray[37])>time())&&($mode=="topic"||$mode=="reply"||$mode=="quote")){
			echo template('notice',array('time'=>$configarray[37]));
		}else{//flood control

		if(strlen($body)>$configarray[18]||strlen($subject)>$configarray[25]){
			//length check
			unset($tplcontent);
			$tplcontent['bodylenreq'].=$configarray[18];
			$tplcontent['subjectlenreq'].=$configarray[25];
			$tplcontent['bodylen'].=strlen($body);
			$tplcontent['subjectlen'].=strlen($subject);

			echo template('noticepostlength',$tplcontent);
		}else{
			############# new topic #################
			if(isset($forum)&&!isset($topictime)&&trim($subject)&&trim($body)&&$mode=="topic"){//mode if/then
			//new topic
			unset($tplcontent);

			$post="0";

			$time=time();
			if($importantpost=="on"){
				$topictime="important_$time";
			}else{
				$topictime=$time;
			}

			$pollstatus=1;
			if($poll=="on"){

				if(!$pollitem0||!$pollitem1){
					$pollstatus=0;
					echo template('noticepollminoptions');
				}else{
					unset($pollitems);
					for($n=0;$n<$configarray[24];$n++){
						$varname="pollitem$n";
						if(${$varname}){
							$pollitems=$pollitems.stripslashes(${$varname})."\t";
							$pollvotes=$pollvotes.",";
						}
					}
					createdir("$configarray[2]/$forum/$topictime");
					writedata("$configarray[2]/$forum/$topictime/$post.php","poll",4);
					writedata("$configarray[2]/$forum/$topictime/$post.php",$pollitems,5);
					writedata("$configarray[2]/$forum/$topictime/$post.php",$pollvotes,6);
					writedata("$configarray[2]/$forum/$topictime/$post.php",$pollvoters,7);
					$pollstatus=1;
				}

			}else{
				createdir("$configarray[2]/$forum/$topictime");
			}

			if($attachmentfile&&$attachmentfile!=="none"){
				unset($attachstatus);
				echo addattachment();

				if($attachstatus==1){
					createdir("$configarray[2]/$forum/$topictime");
					writedata("$configarray[2]/$forum/$topictime/$post.php","attachment",4);
					writedata("$configarray[2]/$forum/$topictime/$post.php",$attachmentfile_name,5);
				}
			}else{
				createdir("$configarray[2]/$forum/$topictime");
			}

			if($attachstatus!==1||$pollstatus==0){
				$tplcontent['notice'].="Post not added, attachment or poll problem<br>Please go back and try again<br>";
				@rmdir("$configarray[2]/$forum/$topictime");
			}else{
				writedata("$configarray[2]/$forum/$topictime/$post.php",$useridarray[$navboardlogin],0);
				writedata("$configarray[2]/$forum/$topictime/$post.php",$time,1);
				writedata("$configarray[2]/$forum/$topictime/$post.php",$subject,2);
				writedata("$configarray[2]/$forum/$topictime/$post.php",$body,3);
				writedata("$configarray[2]/$forum/$topictime/$post.php",$REMOTE_ADDR,9);
				writedata("$configarray[2]/$forum/$topictime/$post.php",$disablebbcode,10);
				writedata("$configarray[2]/$forum/$topictime/$post.php",$disablesmilies,12);

				//increase user post count if forum post increase turned on
				if($forumconfigarray[6]=="on"){
					$posts=$userloggedinarray[6]+1;
					writedata("$configarray[1]/$useridarray[$navboardlogin]/main.php",$posts,6);
				}

				//add last post info to user
				writedata("$configarray[1]/$useridarray[$navboardlogin]/main.php",$time,18);

				$topicidarray=getdata("$configarray[2]/${forum}_topics.php");
				$topicnum=count($topicidarray);
				writedata("$configarray[2]/${forum}_topics.php",$topictime,$topicnum);
				writedata("$configarray[2]/$forum/$topictime.php",$topicnum,3);

				echo template('noticepostadded',array('post'=>"index.php?forum=$forum&topic=$topicnum&page=0"));
				echo template('refresh',array('url'=>"index.php?forum=$forum&topic=$topicnum&page=0"));
			}//attach success check

			echo template('notice',$tplcontent);

			############# quoting post or reply ################
			}elseif(isset($forum)&&isset($topictime)&&trim($body)&&($mode=="quote"||$mode=="reply")){//mode if/then

			@rsort($postarray,SORT_NUMERIC);
			$post=$postarray[0]+1;
			@sort($postarray,SORT_NUMERIC);

			if($attachmentfile&&$attachmentfile!=="none"){
				unset($attachstatus);
				echo addattachment();

				if($attachstatus==1){
					$newname=time();

					//update time of thread and update config file to new time also
					if($importantpost=="on"){
						@rename("$configarray[2]/$forum/$topictime","$configarray[2]/$forum/important_$newname");
						@rename("$configarray[2]/$forum/$topictime.php","$configarray[2]/$forum/important_$newname.php");
						$newtopic="important_$newname";
					}else{
						@rename("$configarray[2]/$forum/$topictime","$configarray[2]/$forum/$newname");
						@rename("$configarray[2]/$forum/$topictime.php","$configarray[2]/$forum/$newname.php");
						$newtopic="$newname";
					}

					writedata("$configarray[2]/$forum/$newname/$post.php","attachment",4);
					writedata("$configarray[2]/$forum/$newname/$post.php",$attachmentfile_name,5);
				}
			}else{
				$newname=time();

				//update time of thread and update config file to new time also
				if($importantpost=="on"){
					@rename("$configarray[2]/$forum/$topictime","$configarray[2]/$forum/important_$newname");
					@rename("$configarray[2]/$forum/$topictime.php","$configarray[2]/$forum/important_$newname.php");
					$newtopic="important_$newname";
				}else{
					@rename("$configarray[2]/$forum/$topictime","$configarray[2]/$forum/$newname");
					@rename("$configarray[2]/$forum/$topictime.php","$configarray[2]/$forum/$newname.php");
					$newtopic="$newname";
				}
			}//if attachment

			if($attachstatus!==1){
				echo  template('noticeattachmentproblem');
			}else{
				writedata("$configarray[2]/$forum/$newtopic/$post.php",$useridarray[$navboardlogin],0);
				writedata("$configarray[2]/$forum/$newtopic/$post.php",$time,1);
				writedata("$configarray[2]/$forum/$newtopic/$post.php",$subject,2);
				writedata("$configarray[2]/$forum/$newtopic/$post.php",$body,3);
				writedata("$configarray[2]/$forum/$newtopic/$post.php",$disablebbcode,10);
				writedata("$configarray[2]/$forum/$newtopic/$post.php",$disablesmilies,12);
				writedata("$configarray[2]/$forum/$newtopic/$post.php",$REMOTE_ADDR,9);

				//add one to users post count
				if($forumconfigarray[6]=="on"){
					$posts=$userloggedinarray[6]+1;
					writedata("$configarray[1]/$useridarray[$navboardlogin]/main.php",$posts,6);
				}

				//add last post info to user
				writedata("$configarray[1]/$useridarray[$navboardlogin]/main.php",$time,18);

				//add rating
				if($rating!==""){
					$ratingarray=explode("\t",$topicconfigarray[1]);
					$ratingarray[0]=$ratingarray[0]+$rating;
					$ratingarray[1]++;
					writedata("$configarray[2]/$forum/$topictime.php",$ratingarray[0]."\t".$ratingarray[1],1);
				}

				writedata("$configarray[2]/${forum}_topics.php",$newtopic,$topicnum);
				if($configarray[42]=="on"){writedata("$configarray[2]/$forum.php",$forumconfigarray[11]+1,11);}//if post count is recorded not counted on fly

				echo template('noticepostadded',array('post'=>"index.php?forum=$forum&topic=$topicnum&page=last"));
				echo template('refresh',array('url'=>"index.php?forum=$forum&topic=$topicnum&page=last"));
			}//end attach success check

			############## editing ####################
			}elseif($mode=="edit"&&trim($body)&&isset($post)&&isset($topictime)&&isset($forum)){//mode if/then

			if(!geteditaccess()){//edit access check (edit mode)
				unset($tplcontent);
				echo template('noticeeditnoaccess');
			}else{//edit access check (edit mode)

			if($post==0&&!trim($subject)){//blank subject on first post
				unset($tplcontent);
				echo template('noticeblanksubject');
			}else{//blank subject on first post

			unset($tplcontent);
			$indpostarray=getdata("$configarray[2]/$forum/$topictime/$post.php");
			$moderatorsarray=explode(",",$forumconfigarray[1]);

			writedata("$configarray[2]/$forum/$topictime/$post.php",$subject,2);
			writedata("$configarray[2]/$forum/$topictime/$post.php",$body,3);
			writedata("$configarray[2]/$forum/$topictime/$post.php",$disablebbcode,10);
			writedata("$configarray[2]/$forum/$topictime/$post.php",$disablesmilies,12);

			if($indpostarray[11]){
				$editarray=explode(",",$indpostarray[11]);
			}
			$editarray[]=$useridarray[$navboardlogin].",".time();
			$editline=implode(",",$editarray);
			writedata("$configarray[2]/$forum/$topictime/$post.php",$editline,11);

			if($deleteattachment=="on"){
				$indpostarray=getdata("$configarray[2]/$forum/$topictime/$post.php");
				@unlink("attachments/$indpostarray[5]");
				writedata("$configarray[2]/$forum/$topictime/$post.php","",4);
				writedata("$configarray[2]/$forum/$topictime/$post.php","",5);
			}

			$attachstatus=1;
			echo addattachment();

			if($attachmentfile&&$attachmentfile!=="none"){
				$attachstatus=addattachment();
				$tplcontent['notice'].=$attacherrorarray[$attachstatus]."<br>";

				if($attachstatus==1){
					$indpostarray=getdata("$configarray[2]/$forum/$topictime/$post.php");
					@unlink("attachments/$indpostarray[5]");
					writedata("$configarray[2]/$forum/$topictime/$post.php","attachment",4);
					writedata("$configarray[2]/$forum/$topictime/$post.php",$attachmentfile_name,5);
				}else{
					echo template('noticeattachmentnotadded');
				}
			}

			if(substr($topictime,0,10)=="important_"){
				$newtopic=substr($topictime,10);
			}else{
				$newtopic=$topictime;
			}

			if($importantpost=="on"){
				@rename("$configarray[2]/$forum/$topictime","$configarray[2]/$forum/important_$newtopic");
				@rename("$configarray[2]/$forum/$topictime.php","$configarray[2]/$forum/important_$newtopic.php");
				$newtopic="important_$newtopic";
			}else{
				@rename("$configarray[2]/$forum/$topictime","$configarray[2]/$forum/$newtopic");
				@rename("$configarray[2]/$forum/$topictime.php","$configarray[2]/$forum/$newtopic.php");
				$newtopic="$newtopic";
			}

			writedata("$configarray[2]/${forum}_topics.php",$newtopic,$topicnum);

			echo template('noticepostedited',array('post'=>"index.php?forum=$forum&topic=$topicnum&page=$page"));

			echo template('refresh',array('url'=>"index.php?forum=$forum&topic=$topicnum&page=$page"));

			}//edit check for blank subject post 0 (first post)

			}//edit access check (edit mode)

			/////delete post or topic////
			}elseif($mode=="deletepost"&&isset($post)&&isset($topic)&&isset($forum)){//mode if/then

			$topictime=topic_numtotime($forum,$topic);
			$indpostarray=getdata("$configarray[2]/$forum/$topictime/$post.php");

			//edit access (delete mode)
			if(geteditaccess()==0){
				unset($tplcontent);
				echo template('noticedeletenoaccess');
			}else{//edit access (delete mode)

			if($post==0){//delete post 0 check

			deletetopic($forum,$topictime);

			unset($tplcontent);
			$tplcontent['notice']="Topic deleted<br>";
			echo template('notice',$tplcontent);

			}else{//deletepost 0 check
			deletepost($forum,$topictime,$post);

			echo template('noticepostdeleted',array('post'=>"index.php?forum=$forum&topic=$topicnum&page=1"));

			echo template('refresh',array('url'=>"index.php?forum=$forum&topic=$topicnum&page=1"));
			}//delete post 0 check

			}//edit access (delete mode)

			}else{

				if(!trim($body)){
					echo template('noticepostnobody');
				}

				if($mode=="topic"&&!trim($subject)){
					echo template('noticepostnosubject');
				}
			}//mode if/then

		}//end length check
		}//end flood control check
		}//step2

	}//normal post access check/invalid check (not edit check)

	}//check ban

}//board close check

require ("footer.php");
?>
